package com.code.ddbond.controller;


import com.code.ddbond.service.UserDetailService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/userdetail")
public class UserDetailController
{
    @Autowired
    private UserDetailService userDetailService;

    @Autowired
    private HttpSession session;

    @GetMapping("/follow")
    public String showFollowPage(Model model)
    {
        return "follow"; // Thymeleaf模板的名称
    }

    @GetMapping("/mainpage")
    public String showmainpage()
    {
        return "mainpage";
    }

    @PostMapping("/follow")
    public String followUser(@RequestParam("userDetailId") Long userDetailId, Model model) {
        try {
            Integer userId = (Integer) session.getAttribute("userId");
            if (userId == null) {
                model.addAttribute("information", "Not logged in.");
            } else {
                userDetailService.increaseFanAndFollowCount(userDetailId, userId);
                model.addAttribute("information", "关注成功");
            }
        } catch (Exception e) {
            model.addAttribute("information", "关注失败");
        }
        return "follow"; // 返回 follow 页面
    }
}
